import React, { FC } from 'react'
import { getVoPrefixAndPostfix } from '@utils/CommUtil'
import { Button } from 'antd'

interface FunReturnVoProps {
  resObj:any,
  parentVo:any,
  onSelectVo:any,
}

const FunReturnVo: FC<FunReturnVoProps> = (props) => {
  const { resObj, parentVo } = props

  const onSelectVo = (_parentVo: any, _resObj: any) => {

    const newParentVo = JSON.parse(JSON.stringify(_parentVo))
    newParentVo.next = _parentVo

    console.info('parentVo=' + JSON.stringify(newParentVo));

    props.onSelectVo(newParentVo, _resObj)
  }

  let { prefix, postfix } = getVoPrefixAndPostfix(resObj)

  return (
    <span>
        {prefix}
      <Button style={{ minWidth: '20%' }} onClick={
        () => {
          const newParentVo = JSON.parse(JSON.stringify(parentVo))
          delete newParentVo.next

          props.onSelectVo(newParentVo, resObj)
        }
      }>{resObj.name || ''}&nbsp;</Button>

      {postfix}

      {(resObj.next || resObj.paradigm == 'yes') && (
        <span>&lt;<FunReturnVo parentVo={resObj} resObj={resObj.next || {}} onSelectVo={onSelectVo}
                               key={resObj.id}/>&gt;</span>
      )}
    </span>
  )

}

export default FunReturnVo

